﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Cryptography;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Text;
using System.Data;


public partial class Admin_Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public int layquyen(string username)
    {
        SqlConnection conn = new SqlConnection
        (ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        conn.Open();
        SqlCommand Cmd = new SqlCommand("Select roleId from tblUser where userName=@user",conn);
        Cmd.Parameters.Add("@user", username);
        int roleid;
        roleid = (int)Cmd.ExecuteScalar();
        conn.Close();
        return roleid;
        

    }
    protected void bntLogin_Click(object sender, EventArgs e)
    {
        
        int i;
        TestUser KTuser = new TestUser();
        if (txtUsername.Text == "" || txtPassword.Text == "")
        {
            lbMsg.Text = "Bạn phải nhập Username và Password !";
        }
        else
        {
            i = KTuser.kiemtra(txtUsername.Text, txtPassword.Text);
            if (i == 0)
            {
                lbMsg.Text = "User name và Password ko hợp lệ vui lòng nhập lại!";
            }
            else
            {
               
                int maquyen = layquyen(txtUsername.Text.ToString());
                Session["Dangnhap"] = txtUsername.Text.ToString();
                if (maquyen == 1)
                {
                    Response.Redirect("~/Admin/Admin.aspx");
                }
                if (maquyen == 2)
                {
                    Response.Redirect("~/home.aspx");
                }
                if (maquyen == 3)
                {
                    Response.Redirect("~/home.aspx");
                }
                if (maquyen == 4)
                {
                    Response.Redirect("~/home.aspx");
                }
            }
        }
    }
}
